package cn.enilu.flash.service.task.job;

import cn.enilu.flash.bean.entity.order.PayOrder;
import cn.enilu.flash.bean.entity.task.HelpTask;
import cn.enilu.flash.bean.vo.query.SearchFilter;
import cn.enilu.flash.security.JwtUtil;
import cn.enilu.flash.service.order.PayOrderService;
import cn.enilu.flash.service.order.TaskOrderService;
import cn.enilu.flash.service.task.HelpTaskService;
import cn.enilu.flash.service.task.JobExecuter;
import cn.enilu.flash.service.task.MoneyInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
* 任务相关任务
* @author: 韦武朝
* @date: 2022/4/8 21:49
* @Return:
*/
@Component
public class HelpTaskJob extends JobExecuter {
    @Autowired
    private HelpTaskService helpTaskService;
    @Autowired
    private TaskOrderService taskOrderService;
    @Autowired
    private PayOrderService payOrderService;
    @Autowired
    private MoneyInfoService moneyInfoService;
    @Override
    public void execute(Map<String, Object> dataMap) throws Exception {
        List<SearchFilter> searchFilters = new ArrayList<>();
        searchFilters.add(new SearchFilter("endTime", SearchFilter.Operator.LT, new Date()));
        searchFilters.add(SearchFilter.build("state", SearchFilter.Operator.NE,5));
        List<HelpTask> endTaskList = helpTaskService.queryAll(searchFilters);
        for(HelpTask helpTask:endTaskList){
            List<SearchFilter> searchFilterList = new ArrayList<>();// 筛选此任务的订单
            searchFilterList.add(new SearchFilter("taskId", SearchFilter.Operator.EQ,helpTask.getId()));
            List<Integer> states = new ArrayList<>();
            states.add(3); // 已完成
            states.add(7); // 已取消
            searchFilterList.add(new SearchFilter("state", SearchFilter.Operator.NOTIN,states)); // 不是 已完成或已取消 的订单
            long count = taskOrderService.count(searchFilterList);
            if(count>0){
                helpTask.setState(6);
                helpTaskService.update(helpTask);
                continue;
            }
            helpTask.setState(5);
            PayOrder payOrder = payOrderService.get(new SearchFilter("taskId", SearchFilter.Operator.EQ, helpTask.getId()));
            if(payOrder!=null && helpTask.getLastNum()>0) {  // 存在付款订单 且 有任务剩余
                // 退钱业务
                BigDecimal price = helpTask.getPrice(); // 单价
                BigDecimal serviceFee = helpTask.getServiceFee(); // 单任务服务费
                Integer lastNum = helpTask.getLastNum(); // 剩余份数
                // 计算要退的钱
                BigDecimal add = price.add(serviceFee); // 单个任务所需退的钱
                BigDecimal refundAmount = add.multiply(new BigDecimal(lastNum)); // 退款金额
                moneyInfoService.setMoneyInfo(helpTask.getId(),helpTask.getCreateBy() , 4, refundAmount);
            }
            helpTaskService.update(helpTask);
        }
        List<HelpTask> lastNumList = helpTaskService.queryAll(new SearchFilter("lastNum", SearchFilter.Operator.LTE, 0));
        for(HelpTask helpTask:lastNumList){
            helpTask.setState(6);
            helpTaskService.update(helpTask);
        }
    }
}
